package com.yksj.healthtalk.ui.server;

import java.util.ArrayList;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.baidu.mapapi.map.ItemizedOverlay;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.OverlayItem;
import com.baidu.mapapi.map.PopupClickListener;
import com.baidu.mapapi.map.PopupOverlay;
import com.baidu.platform.comapi.basestruct.GeoPoint;
import com.yksj.healthtalk.comm.RootFragment;
import com.yksj.healthtalk.ui.R;

public class BMapApiServerFragment extends RootFragment {
	/**
	 * MapView 是地图主控件
	 */
	private MapView mMapView = null;
	/**
	 * 用MapController完成地图控制
	 */
	private MapController mMapController = null;
	private MItemizedOverlay mOverlay = null;
	private PopupOverlay pop = null;
	private ArrayList<OverlayItem> mItems = null;
	private TextView popupText = null;
	private View popupInfo = null;
	private View popupLeft = null;
	private View popupRight = null;
	private Button button = null;
	private MapView.LayoutParams layoutParam = null;
	private OverlayItem mCurItem = null;
	private PopupOverlay mPopupOverlay;
	
	TextView mPopTextView;
	private View mPopItmView;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View inflate = inflater.inflate(R.layout.activity_overlay, null);
		
		mPopItmView = inflater.inflate(R.layout.map_overlay_item_layout,null);
		mPopTextView = (TextView)mPopItmView.findViewById(R.id.textcache);
		
		initView(inflate);
		
		return inflate;
	}

	private void initView(View view) {
		mMapView = (MapView) view.findViewById(R.id.bmapView);
		mMapController = mMapView.getController();// 获取地图控制器
		mMapController.enableClick(true);// 设置地图是否响应点击事件 .
		mMapController.setZoom(14);// 设置地图缩放级别
		mMapView.setBuiltInZoomControls(false);// 显示内置缩放控件
		
		Bundle bundle = getArguments();
		if (bundle != null) {
			initOverlay(bundle.getDouble("lat"), bundle.getDouble("lon"),
					bundle.getString("title"));
		}
		//item 泡泡点击
		final PopupClickListener popListener = new PopupClickListener() {
			@Override
			public void onClickedPopup(int index) {
				//String title = mCurItem.getTitle();
			}
		};
		mPopupOverlay = new PopupOverlay(mMapView,popListener);
	}

	public void initOverlay(double lat, double lon, String title) {
		// 创建自定义overlay
		mOverlay = new MItemizedOverlay(getResources().getDrawable(
				R.drawable.location_mark), mMapView);
		GeoPoint p5 = new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6));
		OverlayItem item5 = new OverlayItem(p5,title, "");
		item5.setMarker(getResources().getDrawable(R.drawable.location_mark));
		mOverlay.addItem(item5);
		mMapController.setCenter(p5);
		// 保存所有item，以便overlay在reset后重新添加
		mItems = new ArrayList<OverlayItem>();
		mItems.addAll(mOverlay.getAllItem());
		// 将overlay 添加至MapView中
		mMapView.getOverlays().add(mOverlay);
		// 刷新地图
		mMapView.refresh();
	}

	/**
	 * 清除所有Overlay
	 * @param view
	 */
	public void clearOverlay(View view) {
		mOverlay.removeAll();
		if (pop != null) {
			pop.hidePop();
		}
		mMapView.removeView(button);
		mMapView.refresh();
	}

	/**
	 * 重新添加Overlay
	 * 
	 * @param view
	 */
	public void resetOverlay(View view) {
		clearOverlay(null);
		// 重新add overlay
		mOverlay.addItem(mItems);
		mMapView.refresh();
	}

	@Override
	public void onPause() {
		// MapView的生命周期与Activity同步，当activity挂起时需调用MapView.onPause()
		mMapView.onPause();
		super.onPause();
	}

	@Override
	public void onResume() {
		// MapView的生命周期与Activity同步，当activity恢复时需调用MapView.onResume()
		mMapView.onResume();
		super.onResume();
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		mMapView.onSaveInstanceState(outState);

	}

	@Override
	public void onDestroy() {
		// MapView的生命周期与Activity同步，当activity销毁时需调用MapView.destroy()
		mMapView.destroy();
		super.onDestroy();
	}

	/**
	 * 从view 得到图片
	 * @param view
	 * @return
	 */
	private  Bitmap getBitmapFromView(View view) {
        view.destroyDrawingCache();
        view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        view.setDrawingCacheEnabled(true);
        Bitmap bitmap = view.getDrawingCache(true);
        return bitmap;
	}
	
	public class MItemizedOverlay extends ItemizedOverlay{
		public MItemizedOverlay(Drawable arg0, MapView arg1) {
			super(arg0, arg1);
		}
		@Override
		protected boolean onTap(int index) {
			OverlayItem item = getItem(index);
			mCurItem = item;
			String title = item.getTitle();
			mPopTextView.setText(title);
			Bitmap[] bitmaps = {getBitmapFromView(mPopItmView)};
			mPopupOverlay.showPopup(bitmaps,item.getPoint(), 32);
			return true;
		}
		/**
		 * 响应点击事件
		 */
		@Override
		public boolean onTap(GeoPoint arg0, MapView arg1) {
			if(mPopupOverlay != null){
				mPopupOverlay.hidePop();
			}
			return false;
		}
	}

}
